const express = require('express');
const router = express.Router();
const { asyncHandle } = require('../getSendResult');
const { createFolder, findFolder, findFolderAllCount, deleteFolder, editFolderName, deleteFileFolder } = require('../../services/folder');
const getRandomColor = require('../../util/getRandomColor');

// 创建文件夹
router.post('/createFolder', asyncHandle(
    async (req, res, next) => {
        const name = req.body.name;
        const color = getRandomColor();
        const isDelete = false;
        const userId = req.id;
        return await createFolder(name, color, isDelete, userId);
    }
));

// 查询文件夹
router.get('/findFolder', asyncHandle(
    async (req, res, next) => {
        const page = req.query.page || 1;
        const limit = req.query.limit || 100;
        return findFolder(page, limit, req.id);
    }
))

// 查询所有文件夹
router.get('/findFolderAllCount', asyncHandle(
    async (req, res, next) => {
        return findFolderAllCount(req.id);
    }
));

// 删除文件夹,同时也要删除该文件夹下的文件
router.get('/deleteFolder', asyncHandle(
    async (req, res, next) => {
        const folderId = req.query.id;
        await deleteFileFolder(folderId);
        return deleteFolder(req.id, folderId);
    }
));

// 修改name,返回一个数组,为受影响的条数
router.get('/editFolderName', asyncHandle(
    async (req, res, next) => {
        const folderId = req.query.id;
        const name = req.query.name;
        return editFolderName(req.id, folderId, name);
    }
))

module.exports = router;